package com.netty.chapter2.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * Description:
 *
 * @author: StriverFeng
 * @date: 2018-9-21
 * Details:
 */
public class EchoServer {
    private final  int port;
    public EchoServer(int port){
        this.port=port;
    }

    public static void main(String[] args) throws Exception {
        if (args.length !=1){
            System.err.println("Usage: "+EchoServer.class.getSimpleName()+"<port>");
            return;
        }
        int port=Integer.parseInt(args[0]);
        new EchoServer(port).start();
    }

    public void start() throws Exception{
        final EchoServerhandler serverHandler=new EchoServerhandler();
        EventLoopGroup group=new NioEventLoopGroup();
        try {
            ServerBootstrap b=new ServerBootstrap();
            b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(serverHandler);
                        }
                    });
            ChannelFuture f=b.bind().sync();
            System.out.println(EchoServer.class.getName()+"start and listening for connections on"+f.channel().localAddress());
            f.channel().closeFuture().sync();

        }finally {
            group.shutdownGracefully().sync();
        }

    }

}
